/*    
 * THis file is part of the xoperator SPARQL/XMPP agent.
 * For further information see: http://xoperator.sf.net
 * Copyright (C) 2007  Jörg Unbehauen  

 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */
package net.sf.xoperator.rdfxmpp;

import java.util.List;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.DOMBuilder;
import org.jdom.output.XMLOutputter;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smack.util.StringUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.dom2_builder.DOM2XmlPullBuilder;

import net.sf.xoperator.Constants;
import net.sf.xoperator.blackboard.NoPlanBlackBoardControl;
import net.sf.xoperator.store.RdfStoreInfo;

/**
 * The general Packet for a query
 * 
 * @author joerg
 * 
 */
public class SPARQLQuery extends IQ {

	public final static String XML_OUTPUT = "XML";

	public final static String TXT_OUTPUT = "TXT";

	public final static String P2P = "P2P";
	
	private boolean localAsked = false;

	private int htl = 1;

	private String query;

	private String outputType = TXT_OUTPUT;

	private List<RdfStoreInfo> storesToAsk;

	private List<String> agentsToAsk;
	
	public List<String> getAgentsToAsk() {
		return agentsToAsk;
	}
	public boolean isLocalAsked() {
		return localAsked;
	}
	public void setLocalAsked(boolean localAsked) {
		this.localAsked = localAsked;
	}

	private SPARQLResult result = null;

	public SPARQLResult getResult() {
		return this.result;
	}

	public void setResult(SPARQLResult result) {
		this.result = result;
	}

	public void setAgentsToAsk(List<String> agentsToAsk) {
		this.agentsToAsk = agentsToAsk;
	}

	public List<RdfStoreInfo> getStoresToAsk() {
		return storesToAsk;
	}

	public void setStoresToAsk(List<RdfStoreInfo> storesToAsk) {
		this.storesToAsk = storesToAsk;
	}

	public String getQuery() {
		return this.query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public SPARQLQuery(String query) {
		super();
		this.query = query;
		this.setType(IQ.Type.GET);
	}

	public void setOutputType(String outputType) {
		this.outputType = outputType;
	}

	public String getOutputType() {
		return outputType;
	}

	@Override
	public String getChildElementXML() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("<query xmlns=\"");
		buffer.append(Constants.SPARQL_XMPP_SPEC_URL);
		buffer.append("\">");
		buffer.append(StringUtils.escapeForXML(query));
		buffer.append("</query>");

		return buffer.toString();
	}

	public static class Provider implements IQProvider {
		private Log log = LogFactory.getLog(SPARQLQuery.Provider.class);

		public IQ parseIQ(XmlPullParser xpp) throws Exception {
			
			Element ele = new DOMBuilder().build(new DOM2XmlPullBuilder().parseSubTree(xpp));
			

			return new SPARQLQuery(ele.getText());
		}
	}
	
	public int getHtl() {
		return htl;
	}
	
	public void setHtl(int htl) {
		this.htl = htl;
	}
	
	public static class IQPaketListener implements PacketListener{
		

		
		private NoPlanBlackBoardControl control;
		
		public IQPaketListener(NoPlanBlackBoardControl control) {
			this.control = control;
		
		}

		public void processPacket(Packet arg0) {
			SPARQLQuery query = (SPARQLQuery) arg0;
			if(query.getType().equals(IQ.Type.GET)){
				query.setOutputType(SPARQLQuery.P2P);
				control.executeQuery(query);
			}
			
			
			
		}

		

	}
	
	public static class IQPacketFilter implements PacketFilter{
		
		public boolean accept(Packet arg0) {
			
			return arg0 instanceof SPARQLQuery;
		}
	}
	
	


}
